package com.ruoyi.web.controller.resturant;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResDish;
import com.ruoyi.resturant.service.IResDishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@Api(tags="菜品管理")
@RestController
@RequestMapping("/api/resturant/dish")
public class ResDishController {

    @Autowired
    private IResDishService resDishService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询菜品列表
     */
    @ApiOperation("查询菜品列表")
    @ApiImplicitParam(name = "name", value = "菜品名称",
            dataType = "string", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String dishName) {
        List<ResDish> list = resDishService.selectResDishByParam(dishName);
        return AjaxResult.success(list);
    }

    /**
     * 获取菜品详细信息
     */
    @ApiOperation(value = "获取菜品详细信息", notes = "TODO：取得某个菜品的详细信息")
    @ApiImplicitParam(name = "id", value = "菜品ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ResDish resDish = resDishService.selectResDishById(id);
        //如果菜品对象在DB中不存在的话 抛出异常
        if(resDish == null){
            throw new BizException(404, "菜品id(" + id + ")不存在");
        }

        String str = JSON.toJSONString(resDish);
        redisCache.setCacheObject(String.valueOf(id),str,1, TimeUnit.HOURS);
        return AjaxResult.success(resDish);
    }

    /**
     * 新增菜品
     */
    @Log(title="菜品管理", businessType = BusinessType.INSERT) //1
    @ApiOperation("新增菜品")
    @PostMapping
    public AjaxResult add(@RequestBody ResDish resDish) {
        return AjaxResult.success(resDishService.insertResDish(resDish));
    }

    /**
     * 修改菜品
     */
    @Log(title="菜品管理", businessType = BusinessType.UPDATE) //1
    @ApiOperation("修改菜品")
    @PutMapping
    public AjaxResult edit(@RequestBody ResDish resDish) {
        // 先上DB查询这个菜品存不存在，不存在的话，我就抛出异常。
        ResDish resDishDb = resDishService.selectResDishById(resDish.getId());
        //如果菜品对象在DB中不存在的话 抛出异常
        if(resDishDb == null){
            throw new BizException(404, "菜品id(" + resDish.getId() + ")不存在");
        }
        //存在的话就更新菜品
        return AjaxResult.success(resDishService.updateResDish(resDish));
    }

    /**
     * 删除菜品
     */
    @Log(title="菜品管理", businessType = BusinessType.DELETE) //1
    @ApiOperation("删除菜品")
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resDishService.deleteResDishById(id));
    }

}
